package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;


//举报界面
public class ReportActivity extends BaseActivity  implements OnCheckedChangeListener,NetDataJson.OnNetDataJsonListener{

    private RadioGroup radioGroup;
    
    
    private LinearLayout edit_LinearLayout;
    
    private String info;
    
    private EditText editText;

    private LoadingDialog loadingDialog = null;

    private NetDataJson netWork;
    
    private String promo_car_id;

    private RadioButton radioButton;

    private boolean fromBuyCar = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report_layout);
        fromBuyCar = getIntent().getBooleanExtra("fromBuycar" , false);
        promo_car_id = getIntent().getStringExtra("promo_car_id");
        loadingDialog = new LoadingDialog(this, "加载中...");
        radioGroup = (RadioGroup) this.findViewById(R.id.group);
        editText = (EditText)this.findViewById(R.id.edit);
        radioGroup.setOnCheckedChangeListener(this);
        radioButton = (RadioButton) this.findViewById(R.id.four_rb);
        edit_LinearLayout = (LinearLayout) findViewById(R.id.linear_edit);

        View backV = findViewById(R.id.title_back);
        if (backV != null) {
            backV.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    PointRecordManage.getInstance().addPoint(mySelf, Piont.SourceReportBack);
                    finish();
                }
            });
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
         switch (arg1) {
         case R.id.frist_rb:
             info = "虚假宣传,车源与描述不对应";
             edit_LinearLayout.setVisibility(View.GONE);
            break;
         case R.id.second_rb:
             info = "店家服务恶劣";
             edit_LinearLayout.setVisibility(View.GONE);
             break;
         case R.id.three_rb:
             info = "无货，虚假信息";
             edit_LinearLayout.setVisibility(View.GONE);
             break;
         case R.id.four_rb:
            // info = editText.getText().toString();
             edit_LinearLayout.setVisibility(View.VISIBLE);
             break;

        default:
            break;
        }        
    }
    
    
    public void submit(View v){
        PointRecordManage.getInstance().addPoint(mySelf, Piont.SourceReportCommit);
        if(radioButton.isChecked()){
            info = editText.getText().toString();
        }
        if(TextUtils.isEmpty(info)){
            Toast.makeText(this, "请输入举报信息！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null != netWork) {
            netWork.cancelTask();
        }
        loadingDialog.show();
        netWork = new NetDataJson(this);
        if(fromBuyCar) {
            netWork.setUrl(API.reports_car);
            netWork.addParam("car_id", promo_car_id);
        }else{
            netWork.setUrl(API.reports);
            netWork.addParam("promo_car_id", promo_car_id);
        }
        netWork.addParam("info", info);

        netWork.request("post");
    }


    @Override
    public void onDataJsonError(String errorMessage) {
        loadingDialog.dismiss();
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();        
    }


    @Override
    public void onDataJsonUpdate(JSONObject json) {
        loadingDialog.dismiss();
        Toast.makeText(this, "举报成功！", Toast.LENGTH_SHORT).show();    
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (netWork !=null){
            netWork.cancelTask();
            netWork = null;
        }
    }
}
